<template>
  <div>
    <el-dialog :title="字典项" :close-on-click-modal="false" :visible.sync="visible">
      <el-button type="primary" @click="addOrUpdateHandle">新增</el-button>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%;margin-top: 20px"
      >
        <!--@selection-change="selectionChangeHandle"-->
        <el-table-column align="center" type="index" header-align="center" width="80" label="序号"></el-table-column>
        <el-table-column prop="type" header-align="center" align="center" label="类型"></el-table-column>
        <el-table-column prop="value" header-align="center" align="center" label="数据值"></el-table-column>
        <el-table-column prop="label" header-align="center" align="center" label="标签名"></el-table-column>
        <el-table-column prop="description" header-align="center" align="center" label="描述"></el-table-column>
        <el-table-column prop="sort" header-align="center" align="center" label="排序"></el-table-column>
        <el-table-column prop="remarks" header-align="center" align="center" label="备注信息"></el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="editOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-dialog
      :title="!id ? '新增' : '修改'"
      :visible.sync="paramsListVisible"
      :close-on-click-modal="false"
      style="width: 100%;"
    >
      <el-form
        size="small"
        :model="dataForm"
        :rules="dataRule"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmit()"
        label-width="80px"
      >
        <el-form-item label="类型" prop="type">
          <el-input v-model="dataForm.type" placeholder="类型" clearable></el-input>
        </el-form-item>
        <el-form-item prop="value" label="数据值">
          <el-input type="number" v-model="dataForm.value" placeholder="数据值" clearable></el-input>
        </el-form-item>
        <el-form-item prop="label" label="标签名">
          <el-input v-model="dataForm.label	" placeholder="标签名" clearable></el-input>
        </el-form-item>
        <el-form-item prop="description" label="描述">
          <el-input v-model="dataForm.description" placeholder="描述" clearable></el-input>
        </el-form-item>
        <el-form-item prop="sort" label="排序">
          <el-input type="number" v-model="dataForm.sort" placeholder="排序" clearable></el-input>
        </el-form-item>
        <el-form-item prop="remarks" label="备注信息">
          <el-input v-model="dataForm.remarks" placeholder="备注信息" clearable></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="paramsListVisible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" :loading="loading">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      visible: false,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      paramsListVisible: false,
      id: '',
      type: '',
      dataForm: {
        id: '',
        dictId: '',
        userId: '',
        value: '',
        type: '',
        label: '',
        description: '',
        sort: '',
        remarks: ''
      },
      dataRule: {
        type: [{ required: true, message: '类型不能为空', trigger: 'blur' }],
        value: [{ required: true, message: '数据值不能为空', trigger: 'blur' }],
        label: [{ required: true, message: '标签名不能为空', trigger: 'blur' }],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ],
        sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
        remarks: [{ required: true, message: '类型不能为空', trigger: 'blur' }]
      },
      dataList: []
    }
  },
  methods: {
    resetData() {
      this.dataForm = {
        id: '',
        dictId: '',
        userId: '',
        value: '',
        type: '',
        label: '',
        description: '',
        sort: '',
        remarks: ''
      }
    },
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: '/dict/type',
        method: 'post',
        data: {
          type: this.dataForm.type
          // 'id': this.dataForm.id,
        }
      }).then(data => {
        if (data && data.code === 200) {
          this.dataList = data.data
        } else {
          this.dataList = []
        }
        this.dataListLoading = false
      })
    },
    addOrUpdateHandle() {
      this.paramsListVisible = true
      this.resetData()
      this.dataForm.type = this.type
    },
    editOrUpdateHandle(id) {
      this.paramsListVisible = true
      this.resetData()
      if (id) {
        this.$http({
          url: `/dict/item`,
          method: 'post',
          data: { id: id }
        }).then(data => {
          if (data && data.code === 200) {
            this.dataForm = data.data
          }
        })
      }
    },
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id
          })
      this.$confirm(`确定对进行[${ids ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http({
            url: '/dict/item/delete',
            method: 'post',
            data: { ids: ids }
          }).then(data => {
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.getDataList()
            } else {
              this.$message.error(data.msg)
            }
          })
        })
        .catch(() => {})
    },

    init(id, type) {
      this.id = id || 0
      this.type = type
      this.dataForm.dictId = id || 0
      this.dataForm.type = type
      this.visible = true
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    // selectionChangeHandle (val) {
    //   this.dataListSelections = val
    // },

    // 字典项修改
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.loading = true
          this.$http({
            url: `/dict/item/${!this.dataForm.id ? 'save' : 'update'}`,
            method: 'post',
            data: this.dataForm
          }).then(data => {
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.paramsListVisible = false
              // this.visible = false
              // this.$emit('refreshDataList')
              this.getDataList()
            } else {
              this.$message.error(data.msg)
            }
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
